﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using System.ComponentModel;

namespace BitBox.Framework.Util
{
    public class LinqHelper<TInfo, TResult>
    {
        public static void ResultToInfo(TResult result, TInfo info)
        {
            if (result == null) return;
            Type infoType = info.GetType();
            foreach (PropertyInfo resultPI in result.GetType().GetProperties())
            {
                PropertyInfo infoPI = infoType.GetProperty(resultPI.Name);
                if (infoPI != null)
                    infoPI.SetValue(info, resultPI.GetValue(result, null), null);
            }
        }

        public static List<TInfo> ListResultToInfo(List<TResult> listResult)
        {
            List<TInfo> listInfo = new List<TInfo>();
            foreach (TResult result in listResult)
            {
                TInfo info = (TInfo)Activator.CreateInstance(typeof(TInfo));
                ResultToInfo(result, info);
                listInfo.Add(info);
            }
            return listInfo;
        }
    }
}
